package cn.alex.sso.project.service.impl;

import cn.alex.sso.project.entity.SysDept;
import cn.alex.sso.project.entity.SysUserDept;
import cn.alex.sso.project.entity.SysUserRole;
import cn.alex.sso.project.mapper.SysUserDeptMapper;
import cn.alex.sso.project.service.SysUserDeptService;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Alex
 * @description 针对表【sys_user_dept(用户和部门关联表)】的数据库操作Service实现
 * @createDate 2024-09-24 17:05:38
 */
@Service
public class SysUserDeptServiceImpl extends ServiceImpl<SysUserDeptMapper, SysUserDept> implements SysUserDeptService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void change(Long userId, List<SysDept> deptList) {
        baseMapper.delete(new LambdaQueryWrapper<SysUserDept>()
                .eq(SysUserDept::getUserId, userId)
        );

        if (CollUtil.isNotEmpty(deptList)) {
            List<SysUserDept> list = new ArrayList<>();
            deptList.forEach(dept -> list.add(new SysUserDept(userId, dept.getId())));
            saveBatch(list);
        }
    }
}




